package com.ray.controller;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ray.bean.CompanyMaterialTableBean;
import com.ray.bean.DatatablesViewPageBean;
import com.ray.pojo.PurchaseOrderDetailPojo;
import com.ray.pojo.attend.SummaryBodyPojo;
import com.ray.service.PurchaseOrderService;
import com.ray.util.AttendanceUtil;
import com.ray.util.ExcelExportUtil;
import com.ray.util.RequestUtil;
import com.ray.util.ResponseUtil;

/**
 * 采购单
* <p>Title: purchaseOrderController</p>  
* <p>Description: </p>  
* @author 李斌  
* @date 2018年9月13日
 */
@Controller
@RequestMapping("/purchaseOrder")
public class PurchaseOrderController {

	@Autowired
	PurchaseOrderService purchaseOrderService; 
	
	/**
	 * 生成订单采购单
	* @date 2018年9月14日 上午8:46:55 
	* @author 李斌
	* @return Map<String,Object> 
	* @throws
	 */
	@RequestMapping("/savePurchaseOrder")
	@ResponseBody
	public Map<String,Object> savePurchaseOrder(HttpServletRequest request) throws ParseException{
		Map<String,Object> map = RequestUtil.getRequestAllParam(request);
		String message = purchaseOrderService.savePurchaseOrder(map);
		return ResponseUtil.ResponseResult("保存成功 "+message);
	}
	
	/**
	 * 生成库存采购
	* @date 2018年9月14日 上午8:47:14 
	* @author 李斌
	* @return Map<String,Object> 
	* @throws
	 */
	@RequestMapping("/savePurchaseStack")
	@ResponseBody
	public Map<String,Object> savePurchaseStack(HttpServletRequest request) throws ParseException{
		Map<String,Object> map = RequestUtil.getRequestAllParam(request);
		String message= purchaseOrderService.savePurchaseStack(map);
		return ResponseUtil.ResponseResult("保存成功 "+message);
	};
	
	/**
	 * 查询采购单
	* @date 2018年9月14日 上午8:47:25 
	* @author 李斌
	* @return Map<String,Object> 
	* @throws
	 */
	@RequestMapping("/getPurchaseOrderList")
	@ResponseBody
	public Map<String,Object> getPurchaseOrderList(HttpServletRequest request){
		Map<String,Object> map = RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(purchaseOrderService.getPurchaseOrderList(map));
	}
	/**
	 * 查询采购单
	* @date 2018年9月14日 上午8:47:25 
	* @author 李斌
	* @return Map<String,Object> 
	* @throws
	 */
	@RequestMapping("/getPurchaseOrderForBackList")
	@ResponseBody
	public Map<String,Object> getPurchaseOrderForBackList(HttpServletRequest request){
		Map<String,Object> map = RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(purchaseOrderService.getPurchaseOrderForBackList(map));
	}
	/**
	 * 查询采购单 导出excel
	* @throws
	 */
	@RequestMapping(value="/exportGetPurchaseOrderList",method=RequestMethod.GET)
	@ResponseBody
	public void exportGetPurchaseOrderList(HttpServletRequest request,HttpServletResponse response)throws ParseException, IOException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{
		Map<String,Object> map = RequestUtil.getRequestAllParam(request);
		//表头字段
		String [] tableHeadFields=null;
		//对应的内容字段
		String [] fields=null;
		if("1".equals(map.get("type").toString())) {
			tableHeadFields=new String[]{"状态","订单编号","客户型号","工厂型号","供方编号","供应商","采购员","物料名称","物料类型","编号","描述","规格型号","物料部门", "材质","单位","含税单价","金额","订单数","投料数(付)","投料数","占用数","采购数量","入库数量","入库日期","税率 %","预计收货期","复期1","复期2","复期3","采购日期","是否打印","制单人","制单日期","审核人","审核时间"};
			fields = new String[]{"statusStr","companyOrderId","articleNo","factoryNo","providerNo","providerName","purchasePeople","materialName","materialType","color","colorDescription","model","materialDeptName","qualityType","unit","price","totalMoney","orderTotal","feedTotal","feedOrderFeedCount","availableCount","purchaseCount",
					"inCount","inCountDate","tax","laterPhaseOne","laterPhaseTwo","laterPhaseThr","laterPhaseFour","purchaseTime","isPrint","createUser","createTime","approvalUser","approvalTime"};
			
		}
		if("2".equals(map.get("type").toString())) {
			tableHeadFields=new String[]{"状态","供方编号","供应商","采购员","物料名称","物料类型","色号","颜色描述","规格型号","材质","单位","含税单价","金额","采购数量","入库数量","入库日期","税率 %","预计收货期","复期1","复期2","复期3","采购日期","是否打印","制单人","制单日期","审核人","审核时间"};
			fields = new String[]{"statusStr","providerNo","providerName","purchasePeople","materialName","materialType","color","colorDescription","model","qualityType","unit","price","totalMoney","purchaseCount",
					"inCount","inCountDate","tax","laterPhaseOne","laterPhaseTwo","laterPhaseThr","laterPhaseFour","purchaseTime","isPrint","createUser","createTime","approvalUser","approvalTime"};
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
		if(map.get("startTime")!=null) {
			Long startTime = Long.parseLong(map.get("startTime").toString());
			map.put("startTime", sdf.format(new Date(startTime)));
		}
		if(map.get("endTime")!=null) {
			Long endTime = Long.parseLong(map.get("endTime").toString());
			map.put("endTime", sdf.format(new Date(endTime)));
		}
		List<PurchaseOrderDetailPojo> tableList = purchaseOrderService.getPurchaseOrderByPage(map).getReturnData();
		ObjectMapper MAPPER = new ObjectMapper();
		//用于打印的数据
		List<PurchaseOrderDetailPojo> list = new ArrayList<PurchaseOrderDetailPojo>();
		//转换对象
		for(PurchaseOrderDetailPojo purchaseOrderDetailPojo : tableList) {
			String jsonStr = MAPPER.writeValueAsString(purchaseOrderDetailPojo);
			PurchaseOrderDetailPojo detail = MAPPER.readValue(jsonStr , PurchaseOrderDetailPojo.class);
			if(detail.getStatus()==0)detail.setStatusStr("未审核");
			if(detail.getStatus()==1)detail.setStatusStr("已审核");
			if("0".equals(detail.getIsPrint()))detail.setIsPrint("未打印");
			if("1".equals(detail.getIsPrint()))detail.setIsPrint("已打印");
			list.add(detail);
		}
		
		
		
		String tempFolder=request.getServletContext().getRealPath("/")+"tempExcelFolder";
		File excelFile=ExcelExportUtil.createExcelFileWithStyle(list,tempFolder,tableHeadFields,fields,"采购单");
		ResponseUtil.downloadfile(response, excelFile, "采购单.xlsx", true);
	}
	
	/**
	 * 审核采购单
	* @date 2018年9月14日 上午8:48:07 
	* @author 李斌
	* @return Map<String,Object> 
	* @throws 
	 */
	@RequestMapping("/approvalPurchaseOrder")
	@ResponseBody
	public Map<String,Object> approvalPurchaseOrder(HttpServletRequest request,int status){
		Map<String,String> map = RequestUtil.getRequestAllParam(request);
		String message = purchaseOrderService.approvalPurchaseOrder(map,status);
		return ResponseUtil.ResponseResult(message);
	}
	
	/**
	 * 采购入库(获取未入库完的采购单)
	* @date 2018年9月14日 下午3:46:36 
	* @author 李斌
	* @return Map<String,Object> 
	* @throws
	 */
	@RequestMapping("/getPurchaseOrderOnPurchaseWarehouseIn")
	@ResponseBody
	public DatatablesViewPageBean<CompanyMaterialTableBean> getPurchaseOrderOnPurchaseWarehouseIn(HttpServletRequest request){
		Map<String,Object> map = RequestUtil.getRequestAllParam(request);
		return purchaseOrderService.getPurchaseOrderOnPurchaseWarehouseIn(map);
	}
	
	/**
	 * 更新复期
	* @date 2018年10月4日 下午4:22:25 
	* @author 李斌
	* @return Map<String,Object> 
	* @throws
	 */
	@RequestMapping("/updatePurchaseLaterPhaseByPurchaseDetailId")
	@ResponseBody
	public Map<String,Object> updatePurchaseLaterPhaseByPurchaseDetailId(HttpServletRequest request){
		Map<String,Object> map = RequestUtil.getRequestAllParam(request);
		purchaseOrderService.updatePurchaseLaterPhaseByPurchaseDetailId(map);
		return ResponseUtil.ResponseResult("修改成功");
	}
	
	/**
	 * 点击微信信息
	* @date 2018年10月4日 下午5:29:17 
	* @author 李斌
	* @return Map<String,Object> 
	* @throws
	 */
	@RequestMapping("/getPurchaseOrderOnWeChat")
	@ResponseBody
	public Map<String,Object> getPurchaseOrderOnWeChat(HttpServletRequest request){
		Map<String,Object> map = RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(purchaseOrderService.getPurchaseOrderOnWeChat(map));
	}
	
	/**
	 * 发送email信息,内容:采购单打印内容
	* @date 2018年10月10日 上午10:09:53 
	* @author 李斌
	* @return Map<String,Object> 
	* @throws
	 */
	@RequestMapping("/sendEmailOnPurchasePrint")
	@ResponseBody
	public Map<String,Object> sendEmailOnPurchasePrint(HttpServletRequest request){
		Map<String,String> map = RequestUtil.getRequestAllParam(request);
		purchaseOrderService.sendEmailOnPurchasePrint(map);
		return ResponseUtil.ResponseResult("已发送");
	}
	
	/**
	 * 获取拖期信息
	* @date 2018年10月24日 上午11:12:44 
	* @author 李斌
	* @return Map<String,Object> 
	* @throws
	 */
	@RequestMapping("/getPurchaseOrderListOnJobSendMsg")
	@ResponseBody
	public Map<String,Object> getPurchaseOrderListOnJobSendMsg(HttpServletRequest request){
		Map<String,Object> map = RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(purchaseOrderService.getPurchaseOrderListOnJobSendMsg(map));
	}
	
	/**
	 * 通过供应商名称和采购单ids获取采购号
	 * @param request
	 * @return
	 */
	@RequestMapping("/getPurchaseNoByProviderNameAndPurchaseDetailIds")
	@ResponseBody
	public Map<String,Object> getPurchaseNoByProviderNameAndPurchaseDetailIds(HttpServletRequest request){
		Map<String,Object> map = RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(purchaseOrderService.getPurchaseNoByProviderNameAndPurchaseDetailIds(map));
	}
	
	
	@RequestMapping("/getPurchaseNoList")
	@ResponseBody
	public Map<String,Object> getPurchaseNoList(HttpServletRequest request){
		Map<String,Object> map = RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(purchaseOrderService.getPurchaseNoList(map));
	}
}
